package com.rhb.af.web.menu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.HashMap;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rhb.af.web.menu.bean.Menu;
import com.rhb.af.web.menu.util.HtmlMenu;
import com.rhb.af.web.menu.util.MenuBuilder;
import com.rhb.af.web.menu.util.MenuPrase;
import com.rhb.af.web.screen.util.ScreenBuilder;


public class MenuServlet extends HttpServlet {
	String encoding = null;
	String builder = null;
	Map<String, Menu> menuDefinitions = new HashMap();

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		encoding = config.getInitParameter("encoding");
		builder = config.getInitParameter("builder");
		String definition = config.getInitParameter("definition");
		String fileRealPath = config.getServletContext().getRealPath("/") + definition;
		menuDefinitions.putAll(MenuPrase.prase(fileRealPath));				
	}
	
	public void destory(){
		encoding = null;
		builder = null;
		menuDefinitions = null;
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException {
		doIt(request,response);
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException {
		doIt(request,response);
	}
	
	public void service(HttpServletRequest request, HttpServletResponse response)
	throws IOException, ServletException {
		doIt(request,response);
	}

	public void doIt(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		MenuBuilder mb;
		try {
			mb = (MenuBuilder)Class.forName(builder).newInstance();
			mb.builder(request,response, menuDefinitions,encoding);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
